<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\api\model;

use think\Db;
use think\Model;

class UserModel extends Model
{
    protected $type = [
    'more' => 'array',
    ];


    public function register($user, $type)
    {
        switch ($type) {
            case 1:
            $result = Db::name("user")->where('user_login', $user['user_login'])->find();
            break;
            case 2:
            $result = Db::name("user")->where('mobile', $user['mobile'])->find();
            break;
            case 3:
            $result = Db::name("user")->where('user_email', $user['user_email'])->find();
            break;
            default:
            $result = 0;
        }
        if (empty($result)) {

            $userId = Db::name("user")->insertGetId($user);
            $data   = Db::name("user")->where('id', $userId)->field('id,mobile')->find();
            $token = cmf_generate_user_token($userId, 'api');
            if (!empty($token)) {
                session('token', $token);
            }
            return $data;
        }
        return 1;
    }
    public function updateInfo($where,$data){
     $rel =  Db::name("user")->where($where)->update($data);
     return $rel ;
   }

    /**
     * 绑定用户手机号
     */
    public function bindingMobile($user)
    {
        $userId          = cmf_get_current_user_id();
        $data ['mobile'] = $user['username'];
        Db::name("user")->where('id', $userId)->update($data);
        $userInfo = Db::name("user")->where('id', $userId)->find();
        cmf_update_current_user($userInfo);
        return 0;
    }
    public function getInfo($where,$field='*'){
        $rel = Db::name("user")->where($where)->field($field)->find();
        return $rel ;
    }
    public function getScore($where,$field='*') {
       $rel = Db::name("user_score_log")->where($where)->field($field)->find();
       return $rel ;
   }
   public function getScoreList($where,$field='*') {
       $rel = Db::name("user_score_log")->where($where)->field($field)->select();
       return $rel ;
   }

 public function getsumScore($where) {
       $rel = Db::name("user_score_log")->where($where)->sum('score');
       return $rel ;
       
   }
    public function getteamReward($where,$field='*'){
       $rel = Db::name("user_tuanduijiangli_list")->where($where)->field($field)->select();
       return $rel ;
       
   }
   public function add_balanceLog($data){
     $rel =  Db::name("user_balance_log")->insert($data);
     return $rel ;
   }
   public function add_scoreLog($data){
     $rel =  Db::name("user_score_log")->insert($data);
     return $rel ;
   }
   public function sum_score($where){
     $rel =  Db::name("user_score_log")->where($where)->sum('score');
     return $rel ;
   }


}
